jetcrab\bytecode\statements/
variable.rs1use crate::ast::Node;
2use crate::bytecode::scope::ScopeManager;
3use crate::vm::instructions::Instruction;
4
5pub trait VariableGenerator {
6 fn generate_variable_declaration(&mut self, node: &Node);
7}
8
9pub trait VariableCore {
10 fn instructions(&mut self) -> &mut Vec<Instruction>;
11 fn visit_node(&mut self, node: &Node);
12}
13
14impl<T> VariableGenerator for T
15where
16 T: VariableCore + ScopeManager,
17{
18 fn generate_variable_declaration(&mut self, node: &Node) {
19 if let Node::VariableDeclaration(decl) = node {
20 for var in &decl.declarations {
21 if let Node::Identifier(name) = &*var.id {
22 if let Some(init) = &var.init {
23 self.visit_node(init);
24 let local_idx = self.get_or_create_local(name);
25 self.instructions().push(Instruction::StoreLocal(local_idx));
26 self.instructions().push(Instruction::LoadLocal(local_idx));
27 } else {
28 let local_idx = self.get_or_create_local(name);
29 self.instructions().push(Instruction::PushUndefined);
30 self.instructions().push(Instruction::StoreLocal(local_idx));
31 self.instructions().push(Instruction::LoadLocal(local_idx));
32 }
33 }
34 }
35 }
36 }
37}